package com.lantian.yjmall.coupon.service.impl;

import com.lantian.common.to.MemberPrice;
import com.lantian.common.to.SkuReductionTo;
import com.lantian.yjmall.coupon.entity.MemberPriceEntity;
import com.lantian.yjmall.coupon.entity.SkuLadderEntity;
import com.lantian.yjmall.coupon.service.MemberPriceService;
import com.lantian.yjmall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantian.common.utils.PageUtils;
import com.lantian.common.utils.Query;

import com.lantian.yjmall.coupon.dao.SkuFullReductionDao;
import com.lantian.yjmall.coupon.entity.SkuFullReductionEntity;
import com.lantian.yjmall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    SkuLadderService skuLadderService;

    @Autowired
    MemberPriceService memberPriceService;



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存sku 关联信息
     * @param reductionTo
     */
    @Override
    public void saveSkuReduction(SkuReductionTo reductionTo) {
        //5.4、sku的优惠、满减等信息；yjmall_sms->  sms_sku_ladder  \ms_sku_full_reduction  \sms_member_price
        SkuLadderEntity ladderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(reductionTo, ladderEntity);
        if(reductionTo.getFullCount() > 0){
            skuLadderService.save(ladderEntity);
        }

        SkuFullReductionEntity fullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(reductionTo, fullReductionEntity);
        if(fullReductionEntity.getFullPrice().compareTo(new BigDecimal("0"))==1){
            this.save(fullReductionEntity);
        }

        List<MemberPrice> memberPrice = reductionTo.getMemberPrice();
        List<MemberPriceEntity> collect = memberPrice.stream().map((item) -> {

            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(reductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setAddOther(1);

            return memberPriceEntity;
        }).filter((item) -> {
            return item.getMemberPrice().compareTo(new BigDecimal("0")) == 1;
        }).collect(Collectors.toList());

        memberPriceService.saveBatch(collect);

    }

}
